//
//  JXTitleButton.m
//  JiaoX
//
//  Created by yinqy on 15/3/4.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "JXTitleButton.h"

@implementation JXTitleButton
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setBackgroundColor:[UIColor redColor]];
        // 内部图标居中
        self.imageView.contentMode = UIViewContentModeCenter;
        // 文字对齐
        self.titleLabel.textAlignment = NSTextAlignmentRight;
        // 文字颜色
        [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        // 字体
        self.titleLabel.font = JXNavigationTitleFont;
        // 高亮的时候不需要调整内部的图片为灰色
        self.adjustsImageWhenHighlighted = NO;
    }
    return self;
}


///**
// *  设置内部图标的frame
// */
//- (CGRect)imageRectForContentRect:(CGRect)contentRect
//{
//    CGFloat imageY = 0;
//    CGFloat imageW = self.height;
//    CGFloat imageH = imageW;
//    CGFloat imageX = self.width - imageW;
//    return CGRectMake(imageX, imageY, imageW, imageH);
//}
//
///**
// *  设置内部文字的frame
// */
//- (CGRect)titleRectForContentRect:(CGRect)contentRect
//{
//    CGFloat titleY = 0;
//    CGFloat titleX = 0;
//    CGFloat titleH = self.height;
//    CGFloat titleW = self.width - self.height;
//    return CGRectMake(titleX, titleY, titleW, titleH);
//}

- (void)setTitle:(NSString *)title forState:(UIControlState)state{
    [super setTitle:title forState:state];
    CGSize size = [title sizeWithFont:self.titleLabel.font];
    [self.titleLabel setSize:size];
    [self.titleLabel setX:0];
    [self.titleLabel setY:0];
    if (self.imageView.image!=nil) {
        //代表有image
        [self.imageView setX:self.titleLabel.width];
        [self.imageView setY:self.titleLabel.height-self.imageView.height];
        [self setSize:CGSizeMake(CGRectGetMaxY(self.imageView.frame), self.titleLabel.height)];
    }else{
        [self setSize:size];
    }
}

-(void)setImage:(UIImage *)image forState:(UIControlState)state{
    [super setImage:image forState:state];
    self.imageView.size = image.size;
    if (![Utils isBlankString:self.titleLabel.text]) {
        JXLog(@"%f",self.titleLabel.width);
        [self.imageView setX:self.titleLabel.width];
        [self.imageView setY:self.titleLabel.height-self.imageView.height];
        [self setNeedsDisplay];
        [self setSize:CGSizeMake(CGRectGetMaxY(self.imageView.frame), self.titleLabel.height)];
    }
}


@end
